package org.jdns.jfreechess.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundByteHandlerAdapter;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.jdns.jfreechess.timeseal.TimesealedMessage;

public class NoTimesealHandler extends ChannelInboundByteHandlerAdapter {
  @Override
  public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBufInputStream stream = new ByteBufInputStream(in);
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    String message;

    try {
      while ((message = reader.readLine()) != null) {
        ctx.nextInboundMessageBuffer().add(
            new TimesealedMessage(System.currentTimeMillis(), message));
      }
    } finally {
      // this is a no-op but it silences findbugs and eclipse warnings
      reader.close();
    }

    ctx.fireInboundBufferUpdated();
  }
}
